package com.huabo.contract.service.impl;

import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.huabo.contract.config.DateBaseConfig;
import com.huabo.contract.mysql.entity.*;
import com.huabo.contract.mysql.mapper.*;
import com.huabo.contract.oracle.entity.*;
import com.huabo.contract.oracle.mapper.*;
import com.huabo.contract.service.TblCirculationService;
import com.huabo.contract.util.HttpClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class TblCirculationServiceImpl implements TblCirculationService {

    @Resource
    private TblCirculationMapper tblCirculationMapper;

    @Resource
    private TblControlmatrixMapper tblControlmatrixMapper;

    @Resource
    private TblRiskeventMapper tblRiskeventMapper;

    @Resource
    private TblCyhwUnitMapper tblCyhwUnitMapper;

    @Resource
    private TblFlowMapper tblFlowMapper;

    @Resource
    private TblContractLendMapper tblContractLendMapper;

    @Resource
    private TblCirculationMySqlMapper tblCirculationMySqlMapper;

    @Resource
    private TblControlmatrixMySqlMapper tblControlmatrixMySqlMapper;

    @Resource
    private TblRiskeventMySqlMapper tblRiskeventMySqlMapper;

    @Resource
    private TblCyhwUnitMySqlMapper tblCyhwUnitMySqlMapper;

    @Resource
    private TblFlowMySqlMapper tblFlowMySqlMapper;

    @Resource
    private TblContractLendMySqlMapper tblContractLendMySqlMapper;

    @Override
    public TblCirculation getOneBytaskid(String taskId) {
        List<TblCirculation> list = this.tblCirculationMapper.findByTaskId(taskId);
        return list != null && list.size() > 0 ? (TblCirculation) this.tblCirculationMapper.findByTaskId(taskId).get(0) : null;
    }

    @Override
    public TblCirculationMySql getOneByMySqltaskid(String taskId) {
        List<TblCirculationMySql> list = this.tblCirculationMySqlMapper.findByTaskId(taskId);
        return list != null && list.size() > 0 ? (TblCirculationMySql) this.tblCirculationMySqlMapper.findByTaskId(taskId).get(0) : null;
    }

    @Override
    public TblCirculation get(String cyid) {
        return this.tblCirculationMapper.findById(cyid);
    }

    @Override
    public TblCirculationMySql getMySql(String cyid) {
        return this.tblCirculationMySqlMapper.findById(cyid);
    }

    @Override
    public void upateTblCirculation(TblCirculation tblCirculation) {
        this.tblCirculationMapper.updateCirculationInfoById(tblCirculation);
    }

    @Override
    public void upateMySqlTblCirculation(TblCirculationMySql tblCirculation) {
        this.tblCirculationMySqlMapper.updateCirculationInfoById(tblCirculation);
    }

    @Override
    public TblCirculation saveTblCirculationnew(String type, String number, String name, String url, BigDecimal loginUser, String buskey, String definitionId, String taskid) {
        TblCirculation c = new TblCirculation();
        c.setCytype(type);
        c.setCycode(number);
        c.setCyname(name);
        c.setCydate(new Date());
        c.setCystate("审批中");
        c.setCyurl(url);
        c.setCystaffid(loginUser.toString());
        c.setBusinesskey(buskey);
        c.setDefinitionid(definitionId);
        c.setTaskid(taskid);
        tblCirculationMapper.saveTblCirculation(c);
        return c;
    }

    @Override
    public TblCirculationMySql saveMySqlTblCirculationnew(String type, String number, String name, String url, BigDecimal loginUser, String buskey, String definitionId, String taskid) {
        TblCirculationMySql c = new TblCirculationMySql();
        c.setCytype(type);
        c.setCycode(number);
        c.setCyname(name);
        c.setCydate(new Date());
        c.setCystate("审批中");
        c.setCyurl(url);
        c.setCystaffid(loginUser.toString());
        c.setBusinesskey(buskey);
        c.setDefinitionid(definitionId);
        c.setTaskid(taskid);
        tblCirculationMySqlMapper.saveTblCirculation(c);
        return c;
    }


    @Override
    public Map<String, Object> findeByTaskId(String taskId, String contractId, String flowId, String token) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            Map<String, Object> dataMap = new HashMap<String, Object>(0);
            TblStaffUtil staff = null;

            List<TblControlmatrix> commonts = tblControlmatrixMapper.findAllByTaskidAll(taskId);
            List<TblRiskevent> risks = tblRiskeventMapper.findByTaskAll(taskId);
            try {
                staff = DealUserToken.parseUserToken(token);
                if (staff == null) {
                    resultMap.put("code", "0");
                    resultMap.put("msg", "用户已失效！");
                    return resultMap;
                }
                TblCyhwUnit aw = tblCyhwUnitMapper.getEntity(Integer.parseInt(contractId.toString()));
                if (flowId == null || flowId == "" || flowId.equals("")) {
                    flowId = aw.getFlowid().toString();
                }
                dataMap.put("aw", aw);
            } catch (Exception e) {
                e.printStackTrace();
            }
            TblFlow flow = tblFlowMapper.findById(flowId);
            TblCirculation cy = tblCirculationMapper.getOneBytaskid(contractId);
            dataMap.put("cy", cy);
            dataMap.put("commonts", commonts);
            dataMap.put("risks", risks);
            dataMap.put("dp", flow);
            dataMap.put("flowid", flowId);
            if (staff.getTrole() != null && staff.getTrole().getRname() != null) {
                dataMap.put("rolename", staff.getTrole().getRname());
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "200");
            resultMap.put("data", dataMap);
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            Map<String, Object> dataMap = new HashMap<String, Object>(0);
            TblStaffUtil staff = null;

            List<TblControlmatrixMySql> commonts = tblControlmatrixMySqlMapper.findAllByTaskidAll(taskId);
            List<TblRiskeventMySql> risks = tblRiskeventMySqlMapper.findByTaskAll(taskId);
            try {
                staff = DealUserToken.parseUserToken(token);
                if (staff == null) {
                    resultMap.put("code", "0");
                    resultMap.put("msg", "用户已失效！");
                    return resultMap;
                }
                TblCyhwUnitMySql aw = tblCyhwUnitMySqlMapper.getEntity(Integer.parseInt(contractId.toString()));
                if (flowId == null || flowId == "" || flowId.equals("")) {
                    flowId = aw.getFlowid().toString();
                }
                dataMap.put("aw", aw);
            } catch (Exception e) {
                e.printStackTrace();
            }
            TblFlowMySql flow = tblFlowMySqlMapper.findById(flowId);
            TblCirculationMySql cy = tblCirculationMySqlMapper.getOneBytaskid(contractId);
            dataMap.put("cy", cy);
            dataMap.put("commonts", commonts);
            dataMap.put("risks", risks);
            dataMap.put("dp", flow);
            dataMap.put("flowid", flowId);
            if (staff.getTrole() != null && staff.getTrole().getRname() != null) {
                dataMap.put("rolename", staff.getTrole().getRname());
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "200");
            resultMap.put("data", dataMap);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> findByLendid(String lendid, String token) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            Map<String, Object> dataMap = new HashMap<String, Object>(0);
            TblStaffUtil user = null;
            try {
                user = DealUserToken.parseUserToken(token);
                TblContractLend lend = tblContractLendMapper.findById(lendid);
                TblCirculation cy = tblCirculationMapper.getOneBytaskid(lendid);
                List<TblMyTask> list = null;
                if (user.getTrole() != null && user.getTrole().getRname() != null) {
                    list = HttpClient.findByTask(user.getTrole().getRname(), user.getStaffid().toString(), 1, 50000);
                } else {
                    list = HttpClient.findByTask("", user.getStaffid().toString(), 1, 50000);
                }
                if (list != null && list.size() > 0) {
                    for (TblMyTask myTask : list) {
                        if (cy != null && myTask.getProcessInstanceId().equals(cy.getBusinesskey())) {
                            Map<String, Object> map = HttpClient.lczxProcessJson(myTask.getTaskId());
                            String object = (String) map.get("data");
                            String[] results = object.replace("\"", "").split(",");
                            if (results.length > 0 && !results[0].equals("")) {
                                List<String> stringB = Arrays.asList(results);
                                dataMap.put("results", stringB);
                                dataMap.put("number", stringB.size());
                            } else {
                                dataMap.put("results", null);
                                dataMap.put("number", 0);
                            }
                            dataMap.put("task", myTask);
                            break;
                        }
                    }
                }

                dataMap.put("cy", cy);
                dataMap.put("tblContractLead", lend);//合同借阅信息
                if (user.getTrole() != null && user.getTrole().getRname() != null) {
                    dataMap.put("rolename", user.getTrole().getRname() != null);
                }
                resultMap.put("code", "1");
                resultMap.put("msg", "成功");
                resultMap.put("data", dataMap);
                return resultMap;
            } catch (Exception e) {
                e.printStackTrace();
            }

            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            Map<String, Object> dataMap = new HashMap<String, Object>(0);
            TblStaffUtil user = null;
            try {
                user = DealUserToken.parseUserToken(token);
                TblContractLendMySql lend = tblContractLendMySqlMapper.findById(lendid);
                TblCirculationMySql cy = tblCirculationMySqlMapper.getOneBytaskid(lendid);
                List<TblMyTaskMySql> list = null;
                if (user.getTrole() != null && user.getTrole().getRname() != null) {
                    list = HttpClient.findByMySqlTask(user.getTrole().getRname(), user.getStaffid().toString(), 1, 1500);
                } else {
                    list = HttpClient.findByMySqlTask("", user.getStaffid().toString(), 1, 1500);
                }
                if (list != null && list.size() > 0) {
                    for (TblMyTaskMySql myTask : list) {
                        if (cy != null && myTask.getProcessInstanceId().equals(cy.getBusinesskey())) {
                            Map<String, Object> map = HttpClient.lczxProcessJson(myTask.getTaskId());
                            String object = (String) map.get("data");
                            String[] results = object.replace("\"", "").split(",");
                            if (results.length > 0 && !results[0].equals("")) {
                                List<String> stringB = Arrays.asList(results);
                                dataMap.put("results", stringB);
                                dataMap.put("number", stringB.size());
                            } else {
                                dataMap.put("results", null);
                                dataMap.put("number", 0);
                            }
                            dataMap.put("task", myTask);
                            break;
                        }
                    }
                }

                dataMap.put("cy", cy);
                dataMap.put("tblContractLead", lend);//合同借阅信息
                if (user.getTrole() != null && user.getTrole().getRname() != null) {
                    dataMap.put("rolename", user.getTrole().getRname() != null);
                }
                resultMap.put("code", "1");
                resultMap.put("msg", "成功");
                resultMap.put("data", dataMap);
                return resultMap;
            } catch (Exception e) {
                e.printStackTrace();
            }

            return resultMap;
        }

    }
}
